package DAO;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import hibernate.User;
import hibernate.Role;
import hibernateUtil.HibernateUtil;
import java.sql.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import org.hibernate.criterion.Restrictions;

public class UserDAO {
	@SuppressWarnings("deprecation")
	public void AddUser(String username,String password,String realname,String department,String position,
			String email,String office_phone,String mobile_phone,String js){
		//添加用户
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		User u=new User();
		u.setUserName(username);
		u.setPassword(password);
		u.setTrueName(realname);
		u.setUnit(department);
		u.setJob(position);
		u.setEmail(email);
		u.setWorkPhone(office_phone);
		u.setMobilePhone(mobile_phone);
		u.setPhone(mobile_phone);
		
		Vector v = new Vector();
		int startlength=0,endlength,end;
		String id;
		endlength=js.indexOf(".");
		end=js.lastIndexOf(".");
	    id=js.substring(0,endlength);
		System.out.println(id);
		v.add(id);
		while(endlength!=end){
			startlength=js.indexOf(",",startlength+1);
			endlength=js.indexOf(".",endlength+1);
			id=js.substring(startlength+1, endlength);
			v.add(id);
		}
		int sizeofv = v.size(),rid;
		for(int i=0;i<sizeofv;i++){
			rid=Integer.parseInt((String)v.get(i));
			v.add(session.createCriteria(Role.class).add(Restrictions.like("roleID",rid)).list());
		}
		Role r;
		for(int i=sizeofv;i<v.size();i++){
			List l1=(List)v.get(i);
			r=((Role)l1.get(0));
			u.getRoles().add(r);
		}
		session.save(u);
		trans.commit();
	}
	
	public Vector searchUser(String start,String limit,String userName,String trueName)
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		Vector vt = new Vector();
		
		String str = "from User where ";
		
		if(!"".equals(userName))
		{
			str = str + "userName=:userName ";
		}
		if(!"".equals(trueName))
		{
			str = str + " or trueName=:trueName ";
		}
		
		Query query = session.createQuery(str);    //查询数据库
		
		if(!"".equals(userName))
		{
			query.setString("userName", userName);
		}
		if(!"".equals(trueName))
		{
			query.setString("trueName", trueName);
		}
		
		int count = (query.list()).size();
		vt.add(count);
		
		query.setFirstResult(Integer.parseInt(start));    //查询记录从这一条开始；
		query.setMaxResults(Integer.parseInt(limit)); 	  //查询记录限制条数；

		List l = query.list();
		vt.add(l);
		trans.commit();
		
		return vt;
	}
	
	public Vector getAll(String start,String limit)
	{
		Vector vt = new Vector();
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		Query query = session.createQuery("from User");
		int count = (query.list()).size();       //得到查询总数目；
		vt.add(count);
		
		query.setFirstResult(Integer.parseInt(start));    //查询记录从这一条开始；
		query.setMaxResults(Integer.parseInt(limit)); 	  //查询记录限制条数；
		
		List l = query.list();               //查询总数目中的limit条记录出来；
		vt.add(l);
		trans.commit();	
		return vt;
	}
	
	public void UpdateUser(String userid,String username,String password,
			               String realname,String department,String position,
			               String email,String office_phone,String mobile_phone,
			               String work_phone,String js){
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		Query q = session.createQuery("update User set userName=:userName,password=:password," +
				"trueName=:trueName,phone=:phone,unit=:unit,job=:job," +
				"email=:email,workPhone=:workPhone,mobilePhone=:mobilePhone where userID=:userID");
		q.setString("userName",username);
		q.setString("password",password);
		q.setString("trueName",realname);
		q.setString("phone",office_phone);
		q.setString("unit",department);
		q.setString("job",position);
		q.setString("email",email);
		q.setString("workPhone",work_phone);
		q.setString("mobilePhone",mobile_phone);
		q.setString("userID",userid);
		
		q.executeUpdate();
		trans.commit();
		
		Session session2 = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans2 = session2.beginTransaction();	
		Vector v = new Vector();
		int startlength = 0,endlength=0,end;
		String id;
		if(js != ""){
			end=js.lastIndexOf(" ");
			startlength = js.indexOf(" ", 0);
			id = js.substring(0,startlength);
			v.add(id);
			if(startlength != end){
				while(endlength != end){
					endlength = js.indexOf(" ", startlength+1);
					id=js.substring(startlength+1, endlength);
					v.add(id);
					startlength = endlength;
				}
			}
		}
		for(int i=0;i<v.size();i++)
			System.out.println("...................."+(String)v.get(i));
		System.out.println("........................"+userid);
		User user;
		Role role;
		List l = session2.createCriteria(User.class).add(Restrictions.like("userID",Integer.parseInt(userid))).list();
		user = (User)l.get(0);
		Set<Role> sr = user.getRoles();
	    Iterator sit = sr.iterator();
	    for(;sit.hasNext();)
	    {
	    	role = (Role)sit.next();
	    	sit.remove();
	    	user.getRoles().remove(role);
	    }
	    for(int i=0;i<v.size();i++){
	    	role = (Role)(session2.createCriteria(Role.class).add(Restrictions.like("roleID",Integer.parseInt((String)v.get(i)))).list()).get(0);
	    	user.getRoles().add(role);
	    }
	    session2.save(user);
		trans2.commit();
	}
	
	public void deleteuser(String data){
		Vector v = new Vector();
		int startlength = 0,endlength=0,end;
		String id;
	    end=data.lastIndexOf(" ");
		if(startlength != end){
			System.out.println("..............................");
			while(endlength != end){
				endlength = data.indexOf(" ", startlength+1);
				id=data.substring(startlength+1, endlength);
				v.add(id);
				startlength = endlength;
			}
		}
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		User user;
		Role role;
		for(int i=0;i<v.size();i++){
			List l = session.createCriteria(User.class).add(Restrictions.like("userID",Integer.parseInt((String)v.get(i)))).list();
			user = (User)l.get(0);
			Set<Role> sr = user.getRoles();
		    Iterator sit = sr.iterator();
		    for(;sit.hasNext();)
		    {
		    	role = (Role)sit.next();
		    	sit.remove();
		    	user.getRoles().remove(role);
		    }
		    session.save(user);
		}
		trans.commit();
		
		Session session2 = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans2 = session2.beginTransaction();
		String sql = "delete User where ";
		for(int i=0;i<v.size();i++){
			sql += "userID="+(String)v.get(i);
			if(i!=v.size()-1)
				sql += " or ";
		}
		if(v.size()>0){
			Query q = session2.createQuery(sql);
			q.executeUpdate();
		}
		trans2.commit();
	}
	
	public List searchUser(String userName)
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		Query query = session.createQuery("from User where userName =:userName").setParameter("userName", userName);
		List l = query.list();
		trans.commit();		
		return l;
	}
	public void updateUserInfo(String userId, String username, String password, String realname, 
			  String unit, String job, String email, String mobilePhone, String workPhone){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		Query q = session.createQuery("update User set userName=:userName,password=:password," +
				"trueName=:trueName,unit=:unit,job=:job," +
				"email=:email,workPhone=:workPhone,mobilePhone=:mobilePhone where userID=:userID");
		q.setString("userName",username);
		q.setString("password",password);
		q.setString("trueName",realname);
		q.setString("unit",unit);
		q.setString("job",job);
		q.setString("email",email);
		q.setString("workPhone",workPhone);
		q.setString("mobilePhone",mobilePhone);
		q.setString("userID",userId);
		q.executeUpdate();
		trans.commit();
	}
	public int[] getroleofuser(String username){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		List l = session.createCriteria(User.class).add(Restrictions.like("userName",username)).list();
		int[] quanxian = new int[39];
		User user;
		Role r;
		user = (User)l.get(0);
		Set<Role> sr = user.getRoles();
	    Iterator sit = sr.iterator();
	    for(;sit.hasNext();)
	    {
	    	r = (Role)sit.next();
	    	//sit.remove();
			if(r.getrAssetCreate()==1)
				quanxian[0]=1;
			if(r.getrAssetRead()==1)
			    quanxian[1]=1;
			if(r.getrAssetUpdate()==1)
				quanxian[2]=1;
			if(r.getrAssetDelete()==1)
				quanxian[3]=1;
			if(r.getrAssetDamageCreate()==1)
				quanxian[4]=1;
			if(r.getrAssetDamageRead()==1)
				quanxian[5]=1;
			if(r.getrAssetDamageUpdate()==1)
				quanxian[6]=1;
			if(r.getrAssetDamageDelete()==1)
				quanxian[7]=1;
			
			if(r.getrAssetChangeCreate()==1)
				quanxian[8]=1;
			if(r.getrAssetChangeRead()==1)
				quanxian[9]=1;
			if(r.getrAssetChangeUpdate()==1)
				quanxian[10]=1;
			if(r.getrAssetChangeDelete()==1)
				quanxian[11]=1;
			
			if(r.getrAssetSaleCreate()==1)
				quanxian[12]=1;
			if(r.getrAssetSaleRead()==1)
				quanxian[13]=1;
			if(r.getrAssetSaleUpdate()==1)
				quanxian[14]=1;
			if(r.getrAssetSaleDelete()==1)
				quanxian[15]=1;
			
			if(r.getrAssetServiceCreate()==1)
				quanxian[16]=1;
			if(r.getrAssetServiceRead()==1)
				quanxian[17]=1;
			if(r.getrAssetServiceUpdate()==1)
				quanxian[18]=1;
			if(r.getrAssetServiceDelete()==1)
				quanxian[19]=1;

			if(r.getrPurchaseSupplyCreate()==1)
				quanxian[20]=1;
			if(r.getrPurchaseSupplyRead()==1)
				quanxian[21]=1;
			if(r.getrPurchaseSupplyUpdate()==1)
				quanxian[22]=1;
			if(r.getrPurchaseSupplyDelete()==1)
				quanxian[23]=1;
			if(r.getrPurchaseSupplyApprove()==1)
				quanxian[24]=1;
			
			if(r.getrOldReport()==1)
				quanxian[25]=1;
			if(r.getrChangeReport()==1)
				quanxian[26]=1;
			if(r.getrServiceReport()==1)
				quanxian[27]=1;
			if(r.getrAssetReport()==1)
				quanxian[28]=1;

			if(r.getrCreateUser()==1)
				quanxian[29]=1;
			if(r.getrReadUser()==1)
				quanxian[30]=1;
			if(r.getrUpdateUser()==1)
				quanxian[31]=1;
			if(r.getrDeleteUser()==1)
				quanxian[32]=1;
			
			if(r.getrCreateRole()==1)
				quanxian[33]=1;
			if(r.getrReadRole()==1)
				quanxian[34]=1;
			if(r.getrUpdateRole()==1)
				quanxian[35]=1;
			if(r.getrDeleteRole()==1)
				quanxian[36]=1;
			
			if(r.getrAssetSortDictionary()==1)
				quanxian[37]=1;
			if(r.getrDepartmentDictionary()==1)
				quanxian[38]=1;
	    }
	    session.save(user);
		trans.commit();		
		return quanxian;
	}
	
}
